package com.baiyang.app.ui.mine.exchange;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.baiyang.app.MyApp;
import com.baiyang.app.R;
import com.baiyang.app.databinding.ActivityRecordExchangeBinding;
import com.baiyang.app.model.Exchange;
import com.baiyang.app.model.Goods;
import com.baiyang.app.model.ReadRecord;
import com.baiyang.app.model.User;
import com.baiyang.app.tool.AlertUtil;
import com.baiyang.app.tool.DataTool;
import com.baiyang.app.tool.LoadDataScrollController;
import com.baiyang.app.tool.MyAdapter;
import com.baiyang.app.ui.read.ReadPageActivity;

import java.util.ArrayList;

public class RecordExchangeActivity extends AppCompatActivity implements LoadDataScrollController.OnRecycleRefreshListener{

    private ActivityRecordExchangeBinding binding;
    private RecordExchangeViewModel viewModel;
    private RecordExchangeService service;
    private User user;
    private int pageOn = 1 ;

    private RecyclerView recyclerView;
    private SwipeRefreshLayout mSwipeRefresh;
    private LoadDataScrollController mController;
    private MyAdapter<Exchange> myAdapter ;
    private ProgressDialog pd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_record_exchange);
        viewModel = new ViewModelProvider(this).get(RecordExchangeViewModel.class);
        service = new RecordExchangeService(getApplicationContext(),viewModel);
        user = MyApp.getInstance().getUser();
        recyclerView = binding.recyclerView;
        mSwipeRefresh = binding.refresh;
        this.mController = new LoadDataScrollController(this);
        myAdapter = new MyAdapter<Exchange>(new ArrayList<>(),R.layout.item_record_exchange) {
            @Override
            public void bindView(ViewHolder holder, Exchange obj) {
                holder.setText(R.id.goodName,obj.getGoodName());
                holder.setText(R.id.integral,String.valueOf(obj.getSpendNum()));
                switch (obj.getState()){
                    case "0":{
                        holder.setText(R.id.state,"待处理");
                        break;
                    }
                    case "1":{
                        holder.setText(R.id.state,"已发货");
                        break;
                    }
                    case "2":{
                        holder.setText(R.id.state,"已完成");
                        break;
                    }
                }
                holder.setText(R.id.phone,obj.getPhone());
                holder.setText(R.id.address,obj.getAddress());
                holder.setText(R.id.time,obj.getCreateTime());
            }
        };
        //设置垂直的线性布局管理器，Orientation --> VERTICAL:垂直 HORIZONTAL:水平
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        //StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        //添加分割线
//        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
        this.mSwipeRefresh.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
        this.mSwipeRefresh.setOnRefreshListener(mController);
        this.recyclerView.addOnScrollListener(mController);
        initHandler();
    }

    @Override
    protected void onStart() {
        super.onStart();
        myAdapter.clear();
        pageOn = 1;
        service.getExchangesRecord(pageOn,user.getId());
    }

    private void initHandler() {
        viewModel.getGoods().observe(this,e->{
            String result = e.getString("result");
            String detail = e.getString("detail");
            if("SUCCESS".equals(result)){
                JSONArray list = e.getJSONArray("list");
                if (list != null && list.size() > 0) {
                    myAdapter.addAll(list.toJavaList(Exchange.class));
                }else{
                }
                if(myAdapter.getmData().size() == 0){
                    binding.hint.setVisibility(View.VISIBLE);
                    Toast.makeText(getApplicationContext(), "空空如也~", Toast.LENGTH_SHORT).show();
                }else{
                    binding.hint.setVisibility(View.GONE);
                }
            }else{
                Toast.makeText(getApplicationContext(),detail,Toast.LENGTH_SHORT).show();
            }
        });
        viewModel.getExchangeResult().observe(this,e->{
            String result = e.getString("result");
            String detail = e.getString("detail");
            if("SUCCESS".equals(result)){
                pageOn = 1;
                service.getExchangesRecord(pageOn,user.getId());
            }else{
                Toast.makeText(getApplicationContext(),detail,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void refresh() {
        myAdapter.clear();
        pageOn = 1;
        service.getExchangesRecord(pageOn,user.getId());
        mSwipeRefresh.setRefreshing(false);
        mController.setLoadDataStatus(false);
    }

    @Override
    public void loadMore() {
        //加载更多的接口回调
        pd = new ProgressDialog(this);
        pd.show();
        mSwipeRefresh.postDelayed(new Runnable() {
            @Override
            public void run() {
                service.getExchangesRecord(++pageOn,user.getId());
                //设置数据加载结束的监听状态
                mController.setLoadDataStatus(false);
                pd.dismiss();
            }
        },2000);
    }
}